<?php

class EmployeeController extends BaseController {

	public function login() {
		$this->view->setTitle(BaliwagConstant::PAGE_TITLE_DEFAULT);

		$this->view->setCss('main');
		$this->view->setCss('employee');
		$this->view->setCss('ribbon');
		$this->view->setCss('plugins/smoothDivScroll');
		$this->view->setCss(BaliwagConstant::JQUERY_UI_CSS_LIB);

		$this->view->setJavascript(BaliwagConstant::JQUERY_LIB);
		$this->view->setJavascript(BaliwagConstant::JQUERY_UI_JS_LIB);
		$this->view->setJavascript('plugins/jquery.mousewheel.min');
		$this->view->setJavascript('plugins/jquery.kinetic.js');
		$this->view->setJavascript('plugins/jquery.smoothdivscroll-1.3-min');

		$this->view->template('pages/login/login');
	}

	public function loginProcess() {
		$loginSvc = new LoginService();

		$result = $loginSvc->login($this->post('username'), $this->post('password'));

		if (trim($result->getEmployeeId())==='') {
			header('Location: index.php?c=employee&m=login&e=ERR1001');
		} else {
			$this->createSession($result);
			// log to DB
			$loginSvc->saveLogin($result);
			
			//get alert products
			$alertProds = $loginSvc->getAlertProd();
			$_SESSION['alertProd'] = $alertProds;
			
			header('Location: index.php?c=employee&m=home');
		}
	}

	public function home() {
		$loginSvc = new LoginService();
		$alertProds = $loginSvc->getAlertProd();
		$_SESSION['alertProd'] = $alertProds;
		HomeUI::init();	
	}

	public function logout() {

		//log to DB
		$loginSvc = new LoginService();
		$employee = $_SESSION['employee'];
		$loginSvc->saveLogout($employee);

		session_destroy();

		$this->login();
	}

	private function createSession($result) {
		$_SESSION['employee'] = $result;
	}
}